//
//  XGStatusReferenceContentTableViewCell.swift
//  TodayNews
//
//  Created by monkey on 2020/2/28.
//  Copyright © 2020 itcast. All rights reserved.
//

import UIKit
import Kingfisher
import YYText

// MARK: - XGStatusReferenceContentTableViewCell

class XGStatusReferenceContentTableViewCell: XGBaseStatusTableViewCell
{
    /// 重写父类属性 会自动触发父类didSet
    override var statusViewModel: XGStatusViewModel? {
        didSet {
            referenceContentLabel.attributedText = statusViewModel?.referenceContentAttributedString
            originalUserView.originalUserInfo = statusViewModel?.originalUserInfo
            statusPictureView.thumbImageList = statusViewModel?.thumbImageList
            statusPictureView.largeImagelist = statusViewModel?.largeImagelist
            statusPictureView.isHidden = statusViewModel?.thumbImageList?.count == 0
            statusPictureView.snp.updateConstraints { (make) in
                make.height.equalTo(statusViewModel!.picturesViewSize.height)
            }
        }
    }
    
    override func setUpUI()
    {
        super.setUpUI()
        
        let referenceView = UIView()
        referenceView.backgroundColor = UIColor(white: 0.95, alpha: 1)
        statusPictureView.backgroundColor = referenceView.backgroundColor
        originalUserView.backgroundColor = referenceView.backgroundColor
        contentView.addSubview(referenceView)
        referenceView.snp.makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp.bottom).offset(10)
            make.left.right.equalToSuperview()
        }
        
        referenceView.addSubview(originalUserView)
        referenceView.addSubview(referenceContentLabel)
        referenceView.addSubview(statusPictureView)
        originalUserView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(5)
            make.left.right.equalToSuperview()
            make.height.equalTo(30)
        }
        referenceContentLabel.snp.makeConstraints { (make) in
            make.top.equalTo(originalUserView.snp.bottom).offset(10)
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.height.lessThanOrEqualTo(150)
        }
        statusPictureView.snp.makeConstraints { (make) in
            make.top.equalTo(referenceContentLabel.snp.bottom)
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(0)
        }
    }
    
    /// 引用文字
    private lazy var referenceContentLabel: XGRichTextLabel = {
        let label = XGRichTextLabel()
        label.numberOfLines = 0
        label.preferredMaxLayoutWidth = TodayNews_Screen_Width - 20.0
        return label
    }()
    /// 配图视图
    private lazy var statusPictureView: XGStatusPicturesView = XGStatusPicturesView()
    // 引用用户视图
    private lazy var originalUserView = XGOriginalUserView()
}

// MARK: - XGOriginalUserView

class XGOriginalUserView: UIView
{
    var originalUserInfo: XGUserInfo? {
        didSet {
            if let userIcon = originalUserInfo?.avatar_url {
                userIconImageView.kf.setImage(with: URL(string: userIcon), options: [.processor(RoundCornerImageProcessor(cornerRadius: 15,targetSize: CGSize(width: 30, height: 30),backgroundColor: backgroundColor))])
            }
            
            userNameLabel.text = originalUserInfo?.screen_name
            verifyImageView.isHidden = originalUserInfo?.user_verified == false
            verifiedReasonLabel.text = originalUserInfo?.verified_reason
            verifiedReasonLabel.isHidden = originalUserInfo?.user_verified == false
        }
    }
    override init(frame: CGRect)
    {
        super.init(frame: frame)
        
        addSubview(userIconImageView)
        addSubview(userNameLabel)
        addSubview(verifyImageView)
        addSubview(verifiedReasonLabel)
        userIconImageView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(10)
            make.top.equalToSuperview()
            make.size.equalTo(CGSize(width: 30, height: 30))
        }
        userNameLabel.snp.makeConstraints { (make) in
            make.left.equalTo(userIconImageView.snp.right).offset(10)
            make.centerY.equalTo(userIconImageView)
        }
        verifyImageView.snp.makeConstraints { (make) in
            make.left.equalTo(userNameLabel.snp.right).offset(5)
            make.centerY.equalTo(userIconImageView)
        }
        verifiedReasonLabel.snp.makeConstraints { (make) in
            make.left.equalTo(verifyImageView.snp.right).offset(10)
            make.centerY.equalTo(userIconImageView)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// 用户头像
    private lazy var userIconImageView = UIImageView()
    /// 用户名
    private lazy var userNameLabel = UILabel(text: "测试", textColor: UIColor.black, fontSize: 15, textAlignment: .left)
    /// 认证原因
    private lazy var verifiedReasonLabel = UILabel(text: "测试", textColor: UIColor.lightGray, fontSize: 13, textAlignment: .left)
    /// 认证标识
    private lazy var verifyImageView: UIImageView = UIImageView(image: UIImage(named: "all_v_avatar_18x18_"))
}
